/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
#include <avkon.hrh>
#include <avkon.rsg>
#include <eikmenup.h>
#include <aknappui.h>
#include <eikcmobs.h>
#include <barsread.h>
#include <stringloader.h>
#include <aknnumedwin.h>
#include <eikenv.h>
#include "SmartRoute.rsg"
#include "SmartRouteDocument.h"

// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "SettingItemList.h"
#include "SettingItemList.hrh"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * Construct the CSmartRouteSettingItemList instance
 * @param aCommandObserver command observer
 */ 
CSmartRouteSettingItemList::CSmartRouteSettingItemList(  
		MEikCommandObserver* aCommandObserver )
	: iCommandObserver( aCommandObserver )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
/** 
 * Destroy any instance variables
 */
CSmartRouteSettingItemList::~CSmartRouteSettingItemList()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * Handle system notification that the container's size has changed.
 */
void CSmartRouteSettingItemList::SizeChanged()
	{
	if ( ListBox() ) 
		{
		ListBox()->SetRect( Rect() );
		}
	}

/**
 * Create one setting item at a time, identified by id.
 * CAknSettingItemList calls this method and takes ownership
 * of the returned value.  The CAknSettingItem object owns
 * a reference to the underlying data, which EditItemL() uses
 * to edit and store the value.
 */
CAknSettingItem* CSmartRouteSettingItemList::CreateSettingItemL( TInt aId )
	{
	CEikAppUi* eikapp = static_cast<CEikAppUi*>(CEikonEnv::Static()->AppUi());
	CSmartRouteDocument* doc = static_cast<CSmartRouteDocument*>(eikapp->Document());
	
	switch ( aId )
		{
		case ESettingItemListWidthEditor:
			{			
			CAknIntegerEdwinSettingItem* item = new (ELeave) 
				CAknIntegerEdwinSettingItem(aId, doc->iLineWidth);
			return item;
			}
		case ESettingItemListFrequenecyEditor:
			{			
			CAknIntegerEdwinSettingItem* item = new (ELeave) 
				CAknIntegerEdwinSettingItem(aId, doc->iFrequency);
			return item;
			}
		case ESettingItemListColourEditor:
			{
			CSmartRouteColourSettingItem* item = new (ELeave)
				CSmartRouteColourSettingItem(aId, doc->iColour);
			return item;
			}
		}
		
	return NULL;
	}
	
/**
 * Edit the setting item identified by the given id and store
 * the changes into the store.
 * @param aIndex the index of the setting item in SettingItemArray()
 * @param aCalledFromMenu true: a menu item invoked editing, thus
 *	always show the edit page and interactively edit the item;
 *	false: change the item in place if possible, else show the edit page
 */
void CSmartRouteSettingItemList::EditItemL ( TInt aIndex, TBool aCalledFromMenu )
	{
	CAknSettingItem* item = ( *SettingItemArray() ) [ aIndex ];
	switch ( item->Identifier() )
		{
	// [[[ begin generated region: do not modify [Editing Started Invoker]
	// ]]] end generated region [Editing Started Invoker]
	
		}
	
	CAknSettingItemList::EditItemL( aIndex, aCalledFromMenu );
	
	TBool storeValue = ETrue;
	switch ( item->Identifier() )
		{
	// [[[ begin generated region: do not modify [Editing Stopped Invoker]
	// ]]] end generated region [Editing Stopped Invoker]
	
		}
		
	if ( storeValue )
		{
		item->StoreL();
		SaveSettingValuesL();
		}	
	}
/**
 *	Handle the "Change" option on the Options menu.  This is an
 *	alternative to the Selection key that forces the settings page
 *	to come up rather than changing the value in place (if possible).
 */
void CSmartRouteSettingItemList::ChangeSelectedItemL()
	{
	if ( ListBox()->CurrentItemIndex() >= 0 )
		EditItemL( ListBox()->CurrentItemIndex(), ETrue );
	}

/**
 *	Load the initial contents of the setting items.  By default,
 *	the setting items are populated with the default values from
 * 	the design.  You can override those values here.
 *	<p>
 *	Note: this call alone does not update the UI.  
 *	LoadSettingsL() must be called afterwards.
 */
void CSmartRouteSettingItemList::LoadSettingValuesL()
	{
	// load values into iSettings
	}
	
/**
 *	Save the contents of the setting items.  Note, this is called
 *	whenever an item is changed and stored to the model, so it
 *	may be called multiple times or not at all.
 */
void CSmartRouteSettingItemList::SaveSettingValuesL()
	{
	// store values from iSettings
	}


/** 
 * Handle key event (override)
 * @param aKeyEvent key event
 * @param aType event code
 * @return EKeyWasConsumed if the event was handled, else EKeyWasNotConsumed
 */
TKeyResponse CSmartRouteSettingItemList::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	if ( aKeyEvent.iCode == EKeyLeftArrow || aKeyEvent.iCode == EKeyRightArrow )
		{
		// allow the tab control to get the arrow keys
		return EKeyWasNotConsumed;
		}
	
	return CAknSettingItemList::OfferKeyEventL( aKeyEvent, aType );
	}
				
CSmartRouteColourSettingItem::CSmartRouteColourSettingItem(TInt aIdentifier, TRgb& aColour) :
	CAknSettingItem(aIdentifier),
	iColour(aColour)
	{
	}

CSmartRouteColourSettingItem::~CSmartRouteColourSettingItem()
	{
	delete iSettingText;
	}
	
void CSmartRouteColourSettingItem::EditItemL(TBool /*aCalledFromMenu*/)
	{
	TBool noneExist = EFalse; 
	TBool noneChosen = EFalse; 
	
	CArrayFixFlat<TRgb>* colours = new(ELeave) CArrayFixFlat<TRgb>(7);
	CleanupStack::PushL(colours);
	colours->AppendL(KRgbWhite); 
	colours->AppendL(KRgbBlack); 
	colours->AppendL(KRgbRed);
	colours->AppendL(KRgbGreen);
	colours->AppendL(KRgbYellow);
	colours->AppendL(KRgbMagenta);
	colours->AppendL(KRgbCyan);
	
	CAknColourSelectionGrid *d = CAknColourSelectionGrid::NewL(colours, noneExist, noneChosen, iColour);
	d->ExecuteLD();
	CleanupStack::PopAndDestroy(); //colours 
	}

const TDesC& CSmartRouteColourSettingItem::SettingTextL()
	{
	if(!iSettingText)
		{	
		iSettingText = CEikonEnv::Static()->AllocReadResourceL(R_SETTING_ITEM_LIST_SELECT_COLOUR);
		}
	return *iSettingText;
	}